/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor.Controlador;

import BaseDatos.FachadaBBDD;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * This class creates a Thread for adding users to the database
 *
 * @author isabel
 * @author Jorge Perez
 */
public class AddUserTask extends Thread implements OcaTask {

    private Socket socket;
    private BufferedReader read;
    private PrintWriter write;

    public AddUserTask(Socket socket, BufferedReader read, PrintWriter write) {
        this.write = write;
        this.socket = socket;
        this.read = read;

    }

    @Override
    public void executeTask() {
        this.start();
    }

    public void run() {

        int idUser = -1;

        String nick = null;
        String pass = null;
        String email = null;
        String type_user = null;
        int type_userInt = -1;

        try {
            nick = read.readLine();
            pass = read.readLine();
            email = read.readLine();
            type_user = read.readLine();

        } catch (IOException ex) {
            System.out.println("Problemas al recibir la id");

        }
        try {
            type_userInt = Integer.parseInt(type_user);
        } catch (NumberFormatException e) {
            System.out.println("El tipo de usuario no es un tipo de usuario valido");
        }

        idUser = FachadaBBDD.get_instance().insert_user(nick, pass, email, type_userInt);
        write.println(idUser);

        new TrashTask(socket, read, write);

    }
}
